var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   iStap1:false,
   iStap2:false,
   iStap3:false,
   oneId:'',
   height:'',
   oneTagArray:[],
   boxDisplay:false,
    cardArray:[],
   titleArray:[]
  },
  selected1:function(e)
  {
    this.setData({
      titleArray:[],
    })
    oneId: e.currentTarget.dataset.id,
    console.log('selecet111')
    this.setData({
      boxDisplay: !this.data.boxDisplay,
      iStap1:true,
      iStap2:false,
      iStap3:false
    })
    for(var i=0;i<this.data.oneTagArray[0].twoTag.length;i++){
      var id='titleArray['+i+'].id'
      var title='titleArray['+i+'].title'
      this.setData({
        [id]:this.data.oneTagArray[0].twoTag[i].id,
        [title]:this.data.oneTagArray[0].twoTag[i].name
      })
    }
   

  },
  getVideoTableByTwoCatalog:function(oneId,twoId){
    var that=this
    that.setData({
      cardArray:''
    })
    wx.request({
      url: app.globalData.baseUrl + 'microvideo/catalog/two',
      method: 'get',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      data: {
        catalogOneId: oneId,
        catalogTwoId: twoId
      },
      success: function (res) {
        console.log(res)
        that.setData({
          cardArray: []
        })
        var resData = res.data.data.dataList.data
        for (var i = 0; i < resData.length; i++) {
          var title = 'cardArray[' + i + '].title'
          var prices = 'cardArray[' + i + '].prices'
          var poster = 'cardArray[' + i + '].poster'
          var time = 'cardArray[' + i + '].time'
          var play = 'cardArray[' + i + '].play'
          var commit = 'cardArray[' + i + '].commit'
          var id = 'cardArray[' + i + '].id'
          var coverImg = 'cardArray[' + i + '].coverImg'
          var buy = 'cardArray[' + i + '].buy'
          var t = resData[i].title
        

          if (t.length > 10) {
            t = t.substring(0, 8) + '...'
          }
          var videoTimeMin=parseInt((resData[i].videoAttribute.duration/1000)/60)
          var videoTimeSec = (resData[i].videoAttribute.duration / 1000) %60
          if(videoTimeMin==0){
            videoTimeMin='00'
          }
          
          that.setData({
            [time]:videoTimeMin+':'+videoTimeSec,
            [title]: t,
            [prices]: resData[i].price,
            [play]: resData[i].read,
            [commit]: resData[i].commentList.length,
            [id]: resData[i].id,
            [coverImg]: resData[i].coverImg,
            [buy]: resData[i].isBought
          })
        }
        console.log(that.data.cardArray)
      },
      fail: function (res) {
        console.log(res)
        wx.showToast({
          title: '获取视频数据失败',
        })
      }
    })
  },
  getVideoTable:function(event){
    console.log(event)
    
    this.setData({
      boxDisplay:!this.data.boxDisplay
    })
   
    var id=event.currentTarget.dataset.id
    this.getVideoTableByTwoCatalog(this.data.oneId,id)
   
  },
  selected2: function (e) {
    this.setData({
      titleArray: [],
    })
    console.log('selecet222')
    this.setData({
      oneId:e.currentTarget.dataset.id,
      boxDisplay: !this.data.boxDisplay,
      iStap1: false,
      iStap2: true,
      iStap3: false
    })
    for (var i = 0; i < this.data.oneTagArray[1].twoTag.length; i++) {
      var id = 'titleArray[' + i + '].id'
      var title = 'titleArray[' + i + '].title'
      this.setData({
        [id]: this.data.oneTagArray[1].twoTag[i].id,
        [title]: this.data.oneTagArray[1].twoTag[i].name
      })
    }
  },

  selected3: function (e) {
    this.setData({
      titleArray: [],
    })
    console.log('selecet333')
    this.setData({
      oneId: e.currentTarget.dataset.id,
      boxDisplay: !this.data.boxDisplay,
      iStap1: false,
      iStap2: false,
      iStap3: true
    })
    for (var i = 0; i < this.data.oneTagArray[2].twoTag.length; i++) {
      var id = 'titleArray[' + i + '].id'
      var title = 'titleArray[' + i + '].title'
      this.setData({
        [id]: this.data.oneTagArray[2].twoTag[i].id,
        [title]: this.data.oneTagArray[2].twoTag[i].name
      })
    }
  },
  playVideo: function (event) {
    console.log(event)
   var id=event.currentTarget.dataset.id
   var price=event.currentTarget.dataset.price
   var buy=event.currentTarget.dataset.buy
   var vip=wx.getStorageSync('vip')
   if(vip||buy){
     wx.navigateTo({
       url: '../playVideo/playVideo?id=' + id,
     })
   }else{
     
     wx.showModal({
       title: '支付',
       content: '您是否愿意消耗' + price + '积分观看视频',
       confirmColor: "#02a0e9",
       success: function (res) {
        if(res.confirm){
          wx.request({
            url: app.globalData.baseUrl + 'microvideo/video/' + id,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
            },
            success: function (res) {
              console.log(res)
               if (res.data.msg == '请求成功') {

              wx.navigateTo({
                url: '../playVideo/playVideo?id=' + id,
              })

               } else {
              wx.showModal({
                title: '',
                content: '积分不足,是否充值？',
                success:function(res){
                  if(res.confirm){
                    wx.navigateTo({
                      url: '/geren/yuePage/yuePager',
                    })
                  }
                }
              })
               }

            }
          })
        }
        



       }

     })
   }  
   
   
  },
  
  /*
  *
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    console.log(wx.getStorageSync('sessionID'))
    wx.request({
      url: app.globalData.baseUrl+'microvideo/catalog',
      method:'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      
      },
      success:function(res){
        console.log(res)
        var resData=res.data.data
        for(var i=0;i<resData.length;i++){
          var title = 'oneTagArray['+i+'].title'
          var id='oneTagArray['+i+'].id'
          var iStap='oneTagArray['+i+'].iStap'
          var index='oneTagArray['+i+'].index'
          var twoTag='oneTagArray['+i+'].twoTag'
          that.setData({
            [id]:resData[i].id,
            [title]:resData[i].name,
            [iStap]:'iStap'+(i+1),
            [index]:i+1,
            [twoTag]:resData[i].catalogTwoList
          }) 
          console.log(that.data.oneTagArray[i].iStap)
        }
       
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getVideoTableByTwoCatalog(0,0)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})